//
//  ArticleParent.m
//
//  Created by Heige  on 16/5/10
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "ArticleParent.h"


NSString *const kArticleParentPId = @"pId";
NSString *const kArticleParentPHeadUrl = @"pHeadUrl";
NSString *const kArticleParentUsername = @"username";


@interface ArticleParent ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ArticleParent

@synthesize pId = _pId;
@synthesize pHeadUrl = _pHeadUrl;
@synthesize username = _username;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.pId = [self objectOrNilForKey:kArticleParentPId fromDictionary:dict];
            self.pHeadUrl = [self objectOrNilForKey:kArticleParentPHeadUrl fromDictionary:dict];
            self.username = [self objectOrNilForKey:kArticleParentUsername fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.pId forKey:kArticleParentPId];
    [mutableDict setValue:self.pHeadUrl forKey:kArticleParentPHeadUrl];
    [mutableDict setValue:self.username forKey:kArticleParentUsername];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.pId = [aDecoder decodeObjectForKey:kArticleParentPId];
    self.pHeadUrl = [aDecoder decodeObjectForKey:kArticleParentPHeadUrl];
    self.username = [aDecoder decodeObjectForKey:kArticleParentUsername];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_pId forKey:kArticleParentPId];
    [aCoder encodeObject:_pHeadUrl forKey:kArticleParentPHeadUrl];
    [aCoder encodeObject:_username forKey:kArticleParentUsername];
}

- (id)copyWithZone:(NSZone *)zone
{
    ArticleParent *copy = [[ArticleParent alloc] init];
    
    if (copy) {

        copy.pId = [self.pId copyWithZone:zone];
        copy.pHeadUrl = [self.pHeadUrl copyWithZone:zone];
        copy.username = [self.username copyWithZone:zone];
    }
    
    return copy;
}


@end
